/**
 * @file
 * @auth zhaojing
 * @date 2016/11/24
 */

import './addBankCard.scss';

import Vue from 'vue';
import modal from '../../../grsc-base/component/Modal.vue';
import VerifyCode from '../../component/VerifyCode.vue';
import commonService from '../../common/commonService';
import lib from '../../common/lib';
import tpl from './addBankCard.html';
import help from '../../../grsc-base/component/Help.vue';
import Popover from '../../../grsc-base/component/Popover.vue';
import format from '../../common/format';
import Validator from '../../../grsc-base/component/Validator/main';

const DO_ADD = 'ADD';
const DO_MOD = 'MOD';

export default  Vue.extend({
    name: 'AddBankCard',
    template: tpl,
    data() {
        let title = '';
        if (this.type === DO_ADD) {
            title = '添加銀行卡';
        } else {
            title = '修改銀行卡';
        }

        return {
            title: title,
            isDisabled: true,
            isRequesting: false,
            form:{
                bank: '',
                subBank: '',
                name: '',
                acnumber: '',   // 银行卡号
                payPwd: ''      //资金密码
            },
            rules: {
                bank: {
                    required: true,
                    requiredTip: this.$t('common.comp.addBank.bank.requireTip'),
                },
                subBank: {
                    required: true,
                    requiredTip: this.$t('common.comp.addBank.subBank.requireTip'),
                },
                name: {
                    required: true,
                    requiredTip: this.$t('common.comp.addBank.name.requireTip'),
                    maxlength: 20,
                    maxlengthTip: this.$t('common.comp.addBank.name.maxTip'),
                },
                acnumber: {
                    required: true,
                    requiredTip: this.$t('common.comp.addBank.acnumber.requireTip'),
                    minlength: 8,
                    minlengthTip: this.$t('common.comp.addBank.acnumber.patternTip')
                },
                payPwd: {
                    required: true,
                    requiredTip: this.$t('common.comp.addBank.payPwd.requireTip')
                }
            }

        }
    },
    props: {
        type: {
            type: String,
            default: DO_ADD
        },
        info: {
            type: Object,
            default() {
                return {};
            }
        },
        onSuccess: {
            type: Function,
            default() {
            }
        }
    },
    computed: {


    },
    components: {
        'modal': modal,
        'help': help,
        'popover': Popover,
        VerifyCode,
        Validator
    },
    created(){
        if (this.type === DO_MOD) {
            this.form.bank = this.info.bank;
            this.form.subBank = this.info.subBank;
            this.form.name = this.info.name;
            this.form.acnumber = this.info.acnumber;
        }
    },
    watch: {


    },
    methods: {

        submit(){
            if (this.rules.$checkAll() === false) {
                return;
            }

            let param = {
                bank: this.form.bank,
                subBank: this.form.subBank,
                name: this.form.name,
                acnumber: this.form.acnumber
            };
            let header = {
                'pay-password': this.form.payPwd
            };

            this.isRequesting = true;
            commonService.updateBankPay(param, header).done((rep) => {
                if (rep.code === window.CODE.SUCCESS) {
                    this.hide();
                    window.success(this.$t('common.success'), () => {
                        // 回到
                        this.onSuccess(param);
                    });
                // } else if (rep.code === window.CODE.EXTI_BANK.CODE) {
                //     this.rules.card.$setError(window.CODE.EXTI_BANK.CH);
                } else if (rep.data && rep.data.num) {
                    let num = rep.data.num;
                    if (3 - num > 0) {
                        this.rules.payPwd.$setError(this.$t('common.IncorrectAssetPassTip',{num: 3-num}));
                    } else {
                        this.rules.payPwd.$setError(window.CODE.PAY_PWD_LOCK[this.$i18n.locale]);
                    }
                } else if (rep.code === window.CODE.PAY_PWD_LOCK.CODE) {
                    this.rules.payPwd.$setError(window.CODE.PAY_PWD_LOCK[this.$i18n.locale]);
                } else {
                    window.error(rep.msg);
                }
            }).fail((rep) => {
                window.error(rep.msg);
            }).always(() => {
                this.isRequesting = false;
            });

        },

        hide() {
            this.$emit('hideDialog');
        }
    }
});
